import requestMeeting, { request } from '@/plugins/axios'
import { FetchCalendarListParam, FetchDetailParam } from './interface'
import dayjs from '@/plugins/dayjs'

export const fetchCalendarList = async (startTime: string, endTime: string) => {
	let start_date_time = dayjs(startTime).tz().format('YYYY-MM-DD HH:mm:ss')
	let end_date_time = dayjs(endTime).tz().format('YYYY-MM-DD HH:mm:ss')
	return requestMeeting<FetchCalendarListParam[]>({
		url: '/meeting/load',
		method: 'get',
		params: { start_date_time, end_date_time },
	}).then((res) => (res.success ? res.data : undefined))
}

export const fetchMeetingDetail = async (meetingMoid: string) =>
	request<FetchDetailParam>({
		url: '/meetings/' + meetingMoid + '/all',
		method: 'get',
	}).then((res) => (res.success ? res : undefined))

export const fetchEndMeeting = async (meetingMoid: string, end_room: 0 | 1) =>
	requestMeeting<number>({
		url: `/confs/${meetingMoid}?end_room=${end_room}`,
		method: 'delete',
	}).then((res) => res)

export const fetchDeleteMeeting = async (meetingMoid: string, reason: string) =>
	requestMeeting<number>({
		url: '/booked_confs/' + meetingMoid + '?reason=' + reason,
		method: 'delete',
	}).then((res) => res)

export const fetchDeleteRegularMeeting = async (ids: string[], meetingMoid: string, reason: string) =>
	requestMeeting<number>({
		url: `/regular_meeting/${meetingMoid}`,
		data: { meeting_moids: ids.join(','), reason },
		method: 'delete',
	}).then((res) => res)

/** 判断时间是否有效 */
export const fetchMeetingTimeExpired = async (data: { start_time: string; day: string; meeting_moid?: string }) => {
	let time = dayjs(data.day + ' ' + data.start_time + ':00').tz()
	let start_time = time.format('HH:mm')
	let day = time.format('YYYY-MM-DD')
	return requestMeeting({
		url: '/meetings/expired',
		method: 'get',
		params: { ...data, start_time, day },
	}).then((res) => res)
}
